import { FormOptions } from "./types/types";

export const addRules = (list: any[], requireList?: any[]) => {
  // 空数组就是全部都是必填项
  if (!requireList?.length) {
    list.forEach((item) => {
      item.rules = [
        {
          required: true,
          message: `${item.label}不能为空`,
          trigger: "blur",
        },
      ];
    });
    return list;
  }

  requireList.forEach((prop) => {
    const target = list.find((item) => item.prop === prop) as any;
    if (!target) return;
    if (target.rules) return;
    target.rules = [
      {
        required: true,
        message: `${target.label}不能为空`,
        trigger: "blur",
      },
    ];
  });
  return list;
};

export const useFormData = () => {
  const formOptions = ref<FormOptions[]>([]);
  return {
    formOptions,
    addRules,
  };
};

// 根据 例如requiredFlag属性进行对应的操作-这样 字段多次修改都统一处理了
export const addProperty = (list: any[]) => {
  list.forEach((item: any) => {
    if (item.requiredFlag && !item.rules) {
      item.rules = [
        {
          required: true,
          message: `${item.label}不能为空`,
          trigger: "blur",
        },
      ];
    }
  });
  return list;
};

export const addDisabled = (list: any[], disabledList?: any[]) => {
  // 空数组就是全部都是禁用项
  const resolveItem = (item: any) => {
    if (item.attrs) {
      item.attrs.disabled = true;
    } else {
      item.attrs = {
        disabled: true,
      };
    }
  };
  if (!disabledList?.length) {
    list.forEach((item) => {
      resolveItem(item);
    });
    return list;
  }
  disabledList.forEach((prop) => {
    const target = list.find((item) => item.prop === prop) as any;
    if (!target) return;
    resolveItem(target);
  });
  return list;
};

export const addCanEdit = (list: any[], canEditList?: any[]) => {
  // 空数组就是全部都是禁用项
  const resolveCanItem = (item: any) => {
    if (item.attrs) {
      item.attrs.disabled = false;
    } else {
      item.attrs = {
        disabled: false,
      };
    }
  };
  const resolveDisabledItem = (item: any) => {
    if (item.attrs) {
      item.attrs.disabled = true;
    } else {
      item.attrs = {
        disabled: true,
      };
    }
  };

  if (!canEditList?.length) {
    list.forEach((item) => {
      resolveCanItem(item);
    });
    return list;
  }

  list.forEach((item) => {
    const target = canEditList.find((a) => a === item.prop) as any;
    if (target) {
      resolveCanItem(item);
    } else {
      resolveDisabledItem(item);
    }
  });
  return list;
};

export const searchOptionsResolve = (searchFormOptions: any, handleQuery) => {
  const events = {
    "keyup.enter": (e, item) => {
      handleQuery();
    },
  };

  unref(searchFormOptions).forEach((item) => {
    item.events = events;
    // if (item.attrs) {
    //   item.attrs.class = '!w-140px'
    // } else {
    //   item.attrs = {
    //     class: '!w-140px'
    //   }
    // }

    // const typeList = ['tree-select']
    // if (typeList.includes(item.type)) {
    //   item.attrs.class = '!w-280px'
    // }

    // const labelList = ['产品编码', '产品名称']
    // if (labelList.includes(item.label)) {
    //   item.attrs.class = '!w-240px'
    // }

    // const timeTypeList = ['date-picker']
    // if (timeTypeList.includes(item.type)) {
    //   item.attrs.class = '!w-240px'
    // }

    // // 对时间进行处理
    // const timeFlag = (prop: string) => {
    //   return ['Time', 'Date'].some((item) => prop.includes(item))
    // }
    // if (item.prop && timeFlag(item.prop)) {
    //   if (item.attrs) {
    //     item.attrs.class = '!w-180px'
    //   } else {
    //     item.attrs = {
    //       class: '!w-180px'
    //     }
    //   }
    // }
  });

  return searchFormOptions;
};
